<?php
/**
 * Timfan design模块处理程序
 *
 * @author Tim Fan
 * QQ:1026073477
 * @url http://i-fanr.com/
 */
defined('IN_IA') or exit('Access Denied');

class Micro_eventModuleSite extends WeModuleSite {

	public function doMobileIndex() {
		global $_W,$_GPC;
		$uniacid =$_W['uniacid'];
		$info = pdo_fetchall("SELECT *  FROM ".tablename('micro_eventsetting')." WHERE uniacid=$uniacid");
		$alls = array(); 
		if(!empty($info[0]['eorder']) || $info[0]['eorder'] =="1") { 
			$alls = $this->_pos_order($uniacid);
		}else { 
			$alls = $this->_rev_order($uniacid);
		}
		
		include $this->template('index');
	}
	private function _pos_order($uniacid) { 
		$time = pdo_fetchall("SELECT distinct event_time  FROM ".tablename('micro_event')." WHERE uniacid=$uniacid ORDER BY event_time ASC");
		$years = array();
		for ($i=0;$i<count($time);$i++) { 
			array_push($years, substr($time[$i]['event_time'],0,4));
		}
		$years = array_unique($years);
		$arr = array();
		foreach($years as $row) { 
   			array_push($arr, $row);
		}
		for ($i=0;$i<count($arr);$i++) { 
			$list = pdo_fetchall("SELECT *  FROM ".tablename('micro_event')." WHERE uniacid=$uniacid AND event_time LIKE '".$arr[$i]."%' ORDER BY event_time ASC");
			$result[] = array(
				$arr[$i] => $list,
			); 
		}
		return $result;
	} 

	private function _rev_order($uniacid) { 
		$time = pdo_fetchall("SELECT distinct event_time  FROM ".tablename('micro_event')." WHERE uniacid=$uniacid ORDER BY event_time DESC");
		$years = array();
		for ($i=0;$i<count($time);$i++) { 
			array_push($years, substr($time[$i]['event_time'],0,4));
		}
		$years = array_unique($years);
		$arr = array();
		foreach($years as $row) { 
   			array_push($arr, $row);
		}
		for ($i=0;$i<count($arr);$i++) { 
			$list = pdo_fetchall("SELECT *  FROM ".tablename('micro_event')." WHERE uniacid=$uniacid AND event_time LIKE '".$arr[$i]."%' ORDER BY event_time DESC");
			$result[] = array(
				$arr[$i] => $list,
			); 
		}
		return $result;
	} 
	
	public function doMobileCover1() { 
		global $_W,$_GPC;
		$uniacid =$_W['uniacid'];
		$info = pdo_fetchall("SELECT *  FROM ".tablename('micro_eventsetting')." WHERE uniacid=$uniacid");
		$alls = array();
		if(!empty($info[0]['eorder']) || $info[0]['eorder'] =="1") { 
			$alls = $this->_pos_order($uniacid);
		}else { 
			$alls = $this->_rev_order($uniacid);
		}
		
		include $this->template('index');
	}

	private function _search($year) { 
  		$list = pdo_fetchall("SELECT *  FROM ".tablename('micro_event')." WHERE event_time LIKE '".$years[$i]."%' ORDER BY event_id DESC");
  		return $list;
	}

	public function doWebEvent_manage() {
		global $_W,$_GPC;
		$uniacid =$_W['uniacid'];
		$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		load()->func('tpl');
		if('post' == $op){//添加或修改
			$event_id = intval($_GPC['event_id']);
			if(!empty($event_id)){
			$item = pdo_fetch("SELECT * FROM ".tablename('micro_event')." where event_id=$event_id");
			empty($item)?message('亲,数据不存在！', '', 'error'):"";	
			}
			
 			
			if(checksubmit('submit')){
				empty ($_GPC['event_title'])?message('亲,标题不能为空'):$event_title=$_GPC['event_title'];
			$event_content =$_GPC['event_content'];
			$event_time =$_GPC['event_time'];
			$highlight =$_GPC['highlight'];
			$uniacid =$_W['uniacid'];
				$data = array(
					'event_id'=>$event_id,
					'uniacid'=>$uniacid,
					'event_title' =>$event_title,
					'event_content'=>$event_content,
					'event_time'=>$event_time,
					'highlight' =>$highlight,
					
				);
				
				if(empty($event_id)){
						pdo_insert('micro_event', $data);//添加数据
						message('数据添加成功！', $this->createWebUrl('event_manage', array('op' => 'display')), 'success');
				}else{
						pdo_update('micro_event', $data, array('event_id' => $event_id));
						message('数据更新成功！', $this->createWebUrl('event_manage', array('op' => 'display')), 'success');
				}
				
				
			}else{
				include $this->template('event_manage');
			}
			
			
		}else if('del' == $op){//删除
		
		
			if(isset($_GPC['delete'])){
				$ids = implode(",",$_GPC['delete']);
				$sqls = "delete from  ".tablename('micro_event')."  where event_id in(".$ids.")"; 
				pdo_query($sqls);
				message('删除成功！', referer(), 'success');
			}
			$event_id = intval($_GPC['event_id']);
			$row = pdo_fetch("SELECT event_id FROM ".tablename('micro_event')." WHERE event_id = :event_id", array(':event_id' => $event_id));
			if (empty($row)) {
				//dump($_GPC);
				message('抱歉，数据不存在或是已经被删除！', $this->createWebUrl('event_manage', array('op' => 'display')), 'error');
			}
			pdo_delete('micro_event', array('event_id' => $event_id));
			message('删除成功！', referer(), 'success');
			
		}else if('display' == $op){//显示
			$pindex = max(1, intval($_GPC['page']));
			$psize =20;//每页显示
			
				$condition = "WHERE uniacid=$uniacid";
			if (!empty($_GPC['keyword'])) {
				$condition .= " and  event_title LIKE '%".$_GPC['keyword']."%'  ";
			}
			
			$list = pdo_fetchall("SELECT *  FROM ".tablename('micro_event') ." $condition  ORDER BY event_id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);//分页
			$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('micro_event')." $condition" );
			$pager = pagination($total, $pindex, $psize);
			include $this->template('event_manage');
		}
	}

	public function doMobileAdd_event() { 
		global $_W,$_GPC;
		$openid = $_W['openid'];
		$acid = $_W['acid'];
		load()->classs('weixin.account');
		$accObj= WeixinAccount::create($acid);
		$access_token = $accObj->fetch_token();
		$userInfo = $this->getUserInfo ($access_token, $openid);
		$event_title=$_GPC['event_title'];
		$event_content =$_GPC['event_content'];
		$event_time =$_GPC['event_time'];
		$highlight =$_GPC['highlight'];
		$data = array(
			'event_id'=>$event_id,
			'event_userid'=>$openid,
			'event_title' =>$event_title,
			'event_content'=>$event_content,
			'event_time'=>$event_time,
			'highlight' =>$highlight,	
		);
		if(checksubmit('submit')){ 
			pdo_insert('micro_eventuser', $data);//添加数据
			include $this->template('add_event');
		}		
		include $this->template('add_event');
	}

	public function doMobileUsertree() {
		global $_W,$_GPC;
		$openid = $_W['openid'];
		$uniacid =$_W['uniacid'];
		$acid = $_W['acid'];
		load()->classs('weixin.account');
		$accObj= WeixinAccount::create($acid);
		$access_token = $accObj->fetch_token();
		$userInfo = $this->getUserInfo ($access_token, $openid);
		$info = pdo_fetchall("SELECT *  FROM ".tablename('micro_eventsetting')." WHERE uniacid=$uniacid");
		$alls = array(); 
		if(!empty($info[0]['eorder']) || $info[0]['eorder'] =="1") { 
			$alls = $this->_pos_orders($openid);
		}else { 
			$alls = $this->_rev_orders($openid);
		}
		include $this->template('usertree');
	}

	private function _pos_orders($openid) { 
		$time = pdo_fetchall("SELECT distinct event_time  FROM ".tablename('micro_eventuser')." WHERE event_userid = :openid"." ORDER BY event_time ASC",array(':openid' => $openid));
		$years = array();
		for ($i=0;$i<count($time);$i++) { 
			array_push($years, substr($time[$i]['event_time'],0,4));
		}
		$years = array_unique($years);
		$arr = array();
		foreach($years as $row) { 
   			array_push($arr, $row);
		}
		for ($i=0;$i<count($arr);$i++) { 
			$list = pdo_fetchall("SELECT *  FROM ".tablename('micro_eventuser')." WHERE event_time LIKE '".$arr[$i]."%' AND event_userid = :openid"." ORDER BY event_time ASC",array(':openid' => $openid));
			$result[] = array(
				$arr[$i] => $list,
			); 
		}
		return $result;
	} 

	private function _rev_orders($openid) { 
		$time = pdo_fetchall("SELECT distinct event_time  FROM ".tablename('micro_eventuser')." WHERE event_userid = :openid"." ORDER BY event_time DESC",array(':openid' => $openid));
		$years = array();
		for ($i=0;$i<count($time);$i++) { 
			array_push($years, substr($time[$i]['event_time'],0,4));
		}
		$years = array_unique($years);
		$arr = array();
		foreach($years as $row) { 
   			array_push($arr, $row);
		}
		for ($i=0;$i<count($arr);$i++) { 
			$list = pdo_fetchall("SELECT *  FROM ".tablename('micro_eventuser')." WHERE event_time LIKE '".$arr[$i]."%' AND event_userid = :openid"." ORDER BY event_time DESC",array(':openid' => $openid));
			$result[] = array(
				$arr[$i] => $list,
			); 
		}
		return $result;
	} 

	private function  getUserInfo ($access_token, $openid) 
	{
		load()->func('communication');

        $api_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";

        $content = ihttp_get($api_url);

        $userInfo = @json_decode($content['content'], true);

        return $userInfo;

	}

	public function doWebEvent_param() { 
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		load()->func('tpl');
		$info = pdo_fetch("SELECT * FROM ".tablename('micro_eventsetting'). " WHERE uniacid = :uniacid", array(':uniacid' => $uniacid));
		if(checksubmit('submit')){ 
				$title = $_GPC['title'];
				$copyright = $_GPC['copyright'];
				$eorder = $_GPC['eorder'];
				$skin = $_GPC['skin'];
				$share_content = $_GPC['share_content'];
				$share_title = $_GPC['share_title'];
				$share_icon = $_GPC['share_icon'];
				$infos = array(
					'uniacid' => $uniacid,
					'title' => $title,
					'copyright' => $copyright,
					'eorder' => $eorder,
					'skin' => $skin, 
					'share_content' => $share_content, 
					'share_title' => $share_title, 
					'share_icon' => $share_icon
				);
				if(empty($info)){
						pdo_insert('micro_eventsetting', $infos);//添加数据
						message('数据添加成功！', $this->createWebUrl('event_param'), 'success');
				}else{
						$id = $info['id'];
						pdo_update('micro_eventsetting', $infos, array('id' => $id));
						message('数据更新成功！', $this->createWebUrl('event_param'), 'success');
				}	
		}
		include $this->template('event_param');
	}
}